<?php

class Admin_NewsController extends Kieumau_Controller_Admin
{

    public function init()
    {
    	parent::init();
	}
    
	public function indexAction(){
		$this->_redirect($this->getRequest()->getModuleName(). '/' . $this->getRequest()->getControllerName().'/list');
	}
	
	public function listAction(){
		$newsTable = new Front_Model_DbTable_News();
		
		try{
			$newsList = $newsTable->getAllNews();
		
			$page = $this->_getParam ( 'page', 1 );
			$paginator = Zend_Paginator::factory ( $newsList );
			$paginator->setItemCountPerPage ( 4 );
			$paginator->setCurrentPageNumber ( $page );
			$this->view->newsList = $paginator;
		}catch(Exception $ex){
			echo $ex->getMessage();
			exit;
		}
	}
	
    public function addAction(){
    	
    	$id = $this->getRequest()->getParam('id');
    	$newsTable = new Admin_Model_DbTable_News();
    	$this->view->listNewsType = $this->getNewsTypeAsDropdown();//Create News List
    	
    		if($this->getRequest()->isPost()){
    			 
				$formData = $this->getRequest()->getPost();
				
				$validateParams = array('name'=>'setRequired', 'excerpt'=>'setRequired', 'content'=>'setRequired');
				$error = $this->formValidate($formData, $validateParams);
				
				// If there's no error, The system will save data into database
				if(count($error)==0){
					
					if($formData['id'] != "")
						$id = $formData['id'];
					
					//Xử lý upload image
					if (! empty ( $_FILES ['represent_image'] ['name'] )) {
						$newname = $this->changename ( $_FILES ['represent_image'] ['name'], 'news' );
						try {
							move_uploaded_file ( $_FILES ['represent_image'] ['tmp_name'], NEWS_UPLOAD_PATH . $newname );
							$formData['represent_image']= 'news/'.$newname;
						} catch ( Exception $ex ) {
							echo "Failed to upload file, error :" . $ex->getMessage ();
							exit;
						}
					
						//Delete the old file
						$file_url = NEWS_UPLOAD_PATH . $formData ['represent_image_old'];
						$datontai = file_exists ( $file_url );
						if ($datontai) {
							if (is_file ( $file_url )) { //Check again to be sure 100%
								$file_url_real = NEWS_UPLOAD_PATH . $formData ['image_url_old'];
								unlink ( $file_url_real );
							}
						}
					}//END if !empty...
					else{
						if(!empty($formData['represent_image_old']))
							$formData['represent_image'] = $formData['represent_image_old'];
					}
					
					$this->view->listNewsType = $this->getNewsTypeAsDropdown($formData['type_id']);//Create News List
					//echo "type id la: ".$formData['type_id'];
					
					$this->view->formData = $formData;
					//EDIT
					if($id){
						$newsTable->updateTable($formData, $id);//Update table
					}
					else{
						$id = $newsTable->insertTable($formData);//Insert table
					}
					
					if($id>0){
						$this->view->successfully = true;
					}elseif($id==-1){
						$this->view->error = array($this->language->getMessages('en', 'unsuccessfully'));
					}
				}else{//if exists error, fill form again and notice error
					$this->view->formData = $formData;
					$this->view->error = $error;
				}
			}else{//In case this is not a post
				
				if($id){
					$formData = $newsTable->getAllByTable('id = '.(int)$id);
					if(!empty($formData)){
						$this->view->formData = $formData[0];
						$this->view->listNewsType = $this->getNewsTypeAsDropdown($formData[0]['type_id']);//Create News List
					}
					else
						$this->view->error = array("There's no news with this id!");
				}else
					if((int)$id<0)
						$this->view->error = array("Id of news is invalid!");
			}
		
    	$this->view->id = $id;

    }

    public function deletenewsAction(){
    	$this->_helper->layout->disableLayout ();
    	$this->_helper->viewRenderer->setNoRender ();
    	
    	if($this->getRequest()->isPost()){
    		$id = $this->getRequest()->getPost('id');
    		$newsTable = new Admin_Model_DbTable_News();
    		$newsTable->deleteTableLanguage($id);
    	}
    }
    
    public function activatenewsAction(){
    	$this->_helper->layout->disableLayout ();
    	$this->_helper->viewRenderer->setNoRender ();
    	if($this->getRequest()->isPost()){
    		$id = $this->getRequest()->getPost('id');
    		$newsTable = new Admin_Model_DbTable_News();
    		$newsTable->activateNews($id);
    	}
    }
    
    //return as String
    public function getNewsTypeAsDropdown($selectedValue = ''){
    	
    	$languageTableContent = $this->language->getLanguageDefault();//Get default Language Row
    	
    	$params = array('tableName'=>'news_type_language',//Tên table
    			'where'=>'id_lang = ?',//Điều kiện lọc
    			'params'=>array($languageTableContent['id']),//Tham số truyền vào cho where
    			'value'=>'id',//Trường muốn làm value, trong select
    			'text'=>'name'//Trường muốn làm text, trong select
    	);
    	
    	return $this->createSelectCtrller($params , $selectedValue);
    }
    
}
